﻿#include "stdafx.h"
#include "ScriptWls.h"
#include "thread_control.h"
#include "script.h"
#include "scriptMaple.h"


/**************************************************
 *function:DoWorkWls 乌鲁斯任务
 *input:
 *output:
 *adding:
 *author: lmm
 *date: 2020/11/24
**************************************************/
bool DoWorkWls(int index, int taskIndex) {
	if (LoginClientWls(index, taskIndex)) {
		if (LoginGameWls(index, taskIndex)) {
			return TaskWls(index, taskIndex);
		}
	}
}
//设置列表状态 关闭游戏
bool SetCodeState(int index, int taskIndex,CString ocr,CString state) {
	CDm* dm = g_info[index].dm;
	if (dm->RunScript(ocr)) {
		g_AccountFile.SetString(NumberToString(taskIndex + 1), L"state", state);
		g_main_cwnd->m_list_info.SetItemText(taskIndex, 6, ocr);
		ScriptDelay(index, 10000);
		//KillProcess(L"MapleStory.exe");
		
		return true;
	}
	return false;
}
//乌鲁斯结束退出
void ExitWls(int index) {
	CDm* dm = g_info[index].dm;
	for (size_t k = 0; k < 200; k++)
	{
		if (dm->RunScript(L"乌鲁斯退出1")) {
			dm->KeyDownChar(L"right");
			Sleep(1000);
			for (size_t i = 0; i < 100; i++)
			{
				dm->KeyPressChar(L"up");
			}
			dm->KeyUpChar(L"right");
			//Sleep(RandomNumber(2000, 2500));
			if (!dm->RunScript(L"乌鲁斯地图")) {
				return;
			}
			dm->KeyDownChar(L"left");
			Sleep(1000);
			for (size_t i = 0; i < 100; i++)
			{
				dm->KeyPressChar(L"up");
			}
			dm->KeyUpChar(L"left");
		}
		Sleep(100);
		ShowLog(L"开始退出乌鲁斯");
	}
}

//登录客户端
bool LoginClientWls(int index, int taskIndex) {
	CDm* dm = g_info[index].dm;
	long hwnd = 0;//窗口句柄
	while (g_flag)
	{
		CheckState = false;
		hwnd = dm->GetBindWindow();
		//解除绑定
		if (hwnd > 0) {
			ShowLog(L"解除绑定");
			dm->UnBindWindow();
		}
		//打开游戏
		hwnd = dm->FindWindowEx(0, L"", L"MapleStory");
		ShowLog(L"游戏句柄"+NumberToString(hwnd));
		//如果存在游戏句柄则先关闭游戏
		CString strHwnds;
		CStringArray hwndArr;
		if (hwnd != 0) {
			ShowLog(L"游戏开始窗口句柄存在关闭游戏");
			strHwnds = dm->EnumProcess(L"GameLauncher.exe");
			ShowLog(L"游戏句柄" + strHwnds);
			StringSplit(strHwnds, hwndArr,',');
			for (size_t i = 0; i < hwndArr.GetCount(); i++)
			{
				dm->TerminateProcess(_wtol(hwndArr[i]));
			}
		}
		
		strHwnds = dm->EnumProcess(L"MapleStory.exe");
		ShowLog(L"游戏句柄" + NumberToString(hwnd));
		if (strHwnds != L"" || strHwnds.GetLength() > 0) {
			ShowLog(L"游戏开始窗口句柄存在关闭游戏");
			hwndArr.RemoveAll();
			StringSplit(strHwnds, hwndArr, ',');
			for (size_t i = 0; i < hwndArr.GetCount(); i++)
			{
				dm->TerminateProcess(_wtol(hwndArr[i]));
			}
		}
		ShowLog(L"打开游戏");
		dm->RunScript(L"游戏图标");
		hwnd = dm->FindWindowEx(0, L"", L"MapleStory");
		ScriptDelay(index, 1000);
		dm->KeyPressChar(L"tab");
		ScriptDelay(index, 1000);
		dm->KeyPressChar(L"enter");
		ScriptDelay(index, 10000);

		for (size_t i = 0; i < 150; i++)
		{
			strHwnds = dm->EnumProcess(L"GameLauncher.exe");
			if (strHwnds != L"" || strHwnds.GetLength() > 0) {
				ShowLog(L"登录窗口未关闭重新开始");
				break;
			}

			strHwnds = dm->EnumProcess(L"MapleStory.exe");
			if (strHwnds == L"", strHwnds.GetLength() == 0) {
				ShowLog(L"游戏进程不存在重新开始");
				break;
			}

			hwnd = dm->FindWindowEx(0, L"MapleStoryClass", L"MapleStory");
			ShowLog(L"等待启动.窗口句柄" + NumberToString(hwnd));
			ScriptDelay(index, 1000);
			//int ret = 0;
			if (hwnd != 0) {
				int ret = dm->BindWindowEx(hwnd, L"normal", L"normal", L"normal", L"", 0);
				if (ret == 1) {
					dm->MoveWindow( hwnd, 0, 0);
					CheckState = true;
					ShowLog(L"窗口绑定成功");
					break;
				}
			}
		}

		for (size_t i = 0; i < 30; i++)
		{
			if (dm->RunScript(L"进入登录")) {
				ShowLog(L"游戏开启成功");
				return true;
			}
			ScriptDelay(index, 1000);
		}
	}
	return true;
}

//登录游戏
bool LoginGameWls(int index, int taskIndex,bool isRole,bool isLogin) {
	long x, y;
	CDm* dm = g_info[index].dm;
	dm->UseDict(1);
	bool bFlag = false;
	int nCount = 0;
	while (g_flag)
	{
		ShowLog(L"当前索引" + NumberToString(index));
		SetCodeState(index, taskIndex, L"账号在线", L"5");
		SetCodeState(index, taskIndex, L"账号密码错误", L"2");
		SetCodeState(index, taskIndex, L"二密错误", L"3");
		SetCodeState(index, taskIndex, L"账号验证", L"7");

		if (isRole) {
			int nRoleNum = g_AccountFile.GetInt(NumberToString(taskIndex + 1), L"RoleNum");
			int nZhaNum = g_AccountFile.GetInt(NumberToString(taskIndex + 1), L"ZhaNum");
			if (nRoleNum >= nZhaNum) {
				g_main_cwnd->m_list_info.SetItemText(taskIndex, 6, L"完成");//当前账号置完成
				g_AccountFile.SetString(NumberToString(taskIndex+1), L"state", L"1");
				return false;
			}
		}
		nCount++;
		if (isLogin) {//为假则执行登录 为真则切换角色
			//if (nCount > 30) {
			//	KillProcess(L"MapleStory.exe");
			//	ScriptDelay(index, 1000);
			//	return false;
			//}
			//输入账号密码
			if (dm->RunScript(L"进入登录")) {//到达登录页面
				dm->RunScript(L"输入账号", 1, GetItemText(taskIndex, 1));
				ShowLog(GetItemText(taskIndex, 1));
				ScriptDelay(index, RandomNumber(1000, 1100));
				dm->RunScript(L"输入密码", 1, GetItemText(taskIndex, 2));
				ShowLog(GetItemText(taskIndex, 1));
				ScriptDelay(index, RandomNumber(2000, 2100));
				dm->RunScript(L"点击登录");
			}


			//是否存在保号 不存在直接选择第一账号
			CString strBaoHao = GetItemText(taskIndex, 11);

			if (strBaoHao != L"" && strBaoHao.GetLength() > 0) {
				ScriptDelay(index, RandomNumber(2000, 2100));
				dm->RunScript(L"选择账号", 2, strBaoHao);
			}
			else
			{
				ScriptDelay(index, RandomNumber(2000, 2100));
				dm->RunScript(L"选中账号");
			}

			//选择区服线路
			CString strServer = L"loginChoseServer" + GetItemText(taskIndex, 4) + L".bmp";
			ShowLog(strServer);
			dm->RunScript(L"选择区服", 2, strServer);
			ScriptDelay(index, RandomNumber(1000, 1100));
			//dm->RunScript(L"选择线路25");

			if (dm->RunScript(L"拥挤")) {
				dm->KeyPressChar(L"enter");
			}
			if (dm->RunScript(L"线路窗口")) {
				CString strXian = dm->RunScripts(L"多线选择", L"chSever.bmp");
				int nCount = strXian.Replace(L"|", L"|");
				if (nCount > 0) {
					int nRndXian = RandomNumber(0, nCount);
					CStringArray xianArr;
					StringSplit(strXian, xianArr, '|');
					CStringArray xianPointArr;
					StringSplit(xianArr.GetAt(nRndXian), xianPointArr, ',');
					dm->MoveTo(_wtol(xianPointArr.GetAt(1)) + RandomNumber(5, 15), _wtol(xianPointArr.GetAt(2)) + RandomNumber(5, 10));
					ScriptDelay(index, RandomNumber(500, 1100));
					dm->LeftDoubleClick();
					ScriptDelay(index, RandomNumber(500, 800));
				}
			}
			if (dm->RunScript(L"拥挤")) {
				dm->KeyPressChar(L"enter");
			}

		}

		//返回第一角色页面 切换状态为假
		if (dm->RunScript(L"角色页面", &x, &y) && !bFlag && !isRole) {
			for (size_t k = 0; k < 8; k++)
			{
				dm->FindPic(418, 616, 488, 650, L"Login_Page1.bmp", L"050505", 1, 0, &x,&y);
				//如果第一个存在
				if (x > 0) {
					bFlag = true;
					break;
				}
				else {
					dm->MoveTo(659 + RandomNumber(0, 10), 719 + RandomNumber(0, 3));
					Sleep(RandomNumber(200, 300));
					dm->LeftClick();
					Sleep(RandomNumber(1500, 2000));
				}
			}
		}

		//切换角色为真进行角色切换
		if (dm->RunScript(L"角色页面") && !bFlag && isRole) {
			int a = 0;
			if (g_main_cwnd->m_ZhaKun.GetCheck() == 1) {
				a = g_AccountFile.GetInt(NumberToString(taskIndex + 1), L"RoleNum");
			}
			CString strPage;
			if (a <12) {// 第一页
				strPage = L"Login_Page1.bmp";
			}if (a >= 12 && a < 25) {//2
				strPage = L"Login_Page2.bmp";
			}if (a >= 24 && a < 37) {//2
				strPage = L"Login_Page3.bmp";
			}if (a >= 36 && a < 49) {//2
				strPage = L"Login_Page4.bmp";
			}
			if (a >= 12) {
				if (a % 12 == 0) {
					a = 0;
				}
				else {
					a = a % 12;
				}
			}
			long x1, y1;
			for (size_t k = 0; k < 5; k++)
			{
				//查找页码
				dm->FindPic(405, 613, 450, 653, strPage, L"050505", 1, 0,&x1,&y1);
				if (x1 > 0) {//找到页码
					if (dm->RunScript(L"二密窗口")) {
						bFlag = true;
						break;
					}
					dm->MoveTo(g_Point[a][0]+RandomNumber(0,3), g_Point[a][1]+RandomNumber(0,10));
					dm->LeftDoubleClick();
					Sleep(RandomNumber(1000, 2000));
					bFlag = true;
				}
				else {
					dm->MoveTo(659 + RandomNumber(0, 10), 719 + RandomNumber(0, 3));
					//dm->MoveToEx(659, 719, 10, 3);
					Sleep(RandomNumber(100, 200));
					dm->LeftClick();
					Sleep(RandomNumber(1000, 2000));
				}
			}

		}
		//选择角色输入二密
		if (dm->RunScript(L"角色页面",&x,&y) && bFlag) {
			dm->MoveTo(127+RandomNumber(1,5), 312+RandomNumber(1,8));
			dm->LeftDoubleClick();
			ShowLog(L"到达角色页面");
			ScriptDelay(index, RandomNumber(1300, 1500));
			if (dm->RunScript(L"二密窗口")) {
				ShowLog(L"找到二密窗口");
				CString strKeyWord = GetItemText(taskIndex, 3); //二密
				CString twoCodeImg;
				for (size_t i = 0; i < strKeyWord.GetLength(); i++)
				{
					CString strbmp = strKeyWord.Mid(i, 1) + L".bmp";
					//CString sTwoCode = L"输入二密" + code.Mid(i, 1);
					if (i == twoCodeImg.GetLength() - 1) {
						twoCodeImg += strbmp;
					}
					else {
						twoCodeImg += strbmp + L"|";
					}
				}

				CString strTwoCodeImgs = dm->RunScripts(L"二密查找", twoCodeImg);
				ShowLog(L"二密数据："+ strTwoCodeImgs);
				CStringArray strTwoCodeArr;
				StringSplit(strTwoCodeImgs, strTwoCodeArr, '|');
				//分割二密坐标
				for (size_t i = 0; i < strTwoCodeArr.GetCount(); i++)
				{
					CStringArray strPointArr;
					StringSplit(strTwoCodeArr.GetAt(i), strPointArr, ',');
					ShowLog(strTwoCodeArr.GetAt(i) + L":" + strPointArr.GetAt(1) + L"," + strPointArr.GetAt(2));
					dm->MoveTo(_wtol(strPointArr.GetAt(1)) + RandomNumber(5, 10), _wtol(strPointArr.GetAt(2)) + RandomNumber(5, 10));
					ScriptDelay(index, RandomNumber(200, 280));
					dm->LeftDown();
					ScriptDelay(index, RandomNumber(80, 150));
					dm->LeftUp();
					ScriptDelay(index, RandomNumber(1500, 1700));
					//点击后查找星号数量
					CString strXing = dm->RunScripts(L"星号查找", L"pwdxing.bmp");
					int nCount = strXing.Replace(L"|", L"|");
					if (strXing == L"" && strXing.GetLength() == 0) {//未找到星号重新点击
						i = -1;
						continue;
					}
					else if (nCount>i) {//当前点击的星号大于找到的*则重新点击上一个
						ScriptDelay(index, RandomNumber(1500, 1700));
						dm->KeyPressChar(L"esc");
						ScriptDelay(index, RandomNumber(1500, 1700));
						break;
					}
				}


				if (dm->RunScript(L"二密确认")) {
					for (size_t k = 0; k < 20; k++)
					{
						if (dm->RunScript(L"登录成功")) {
							ScriptDelay(index, 1000);
							return true;
						}
					}
				}
			}

		}
		if (dm->RunScript(L"登录成功")) {
			//ScriptDelay(index, RandomNumber(5500, 7000));
			if (isRole) {
				g_nState = 0;
			}
			return true;
		}

	}
	return false;
}

//切换角色
void SwicthRole(int index, int taskIndex,bool isRole,bool isLogin) {
	long x, y;
	ShowLog(L"切换角色");
	CDm* dm = g_info[index].dm;
	while (g_flag)
	{
		dm->RunScript(L"确定按钮");
		if (dm->RunScript(L"角色页面")) {
			//是否换号
			if (isLogin) {//为真换号
				dm->RunScript(L"退出角色");
				ScriptDelay(index, RandomNumber(1000, 1500));
				dm->RunScript(L"确定退出角色");
			}
			else {//为假换角色
				LoginGameWls(index, taskIndex, isRole, isLogin);
				if (isRole) {
					g_nState = 0;
				}
				return;
			}
		}
		else {
			if (dm->RunScript(L"设置按钮", &x, &y)) {
				dm->MoveToEx(x+5, y+10, 5, 5);
				dm->LeftClick();
				ScriptDelay(index, RandomNumber(3000, 3300));
				dm->MoveTo(x - 20 + RandomNumber(1, 20), y - 35 + RandomNumber(1, 5));
				dm->LeftClick();
				ScriptDelay(index, RandomNumber(1000, 1100));
			}
			if (dm->RunScript(L"点击退出")) {
				ScriptDelay(index, RandomNumber(5000, 5100));
			}
		}
		if (dm->RunScript(L"进入登录")) {
			LoginGameWls(index, taskIndex,isRole,isLogin);
			if (isRole) {
				g_nState = 0;
			}
			return;
		}

	}
}

//乌鲁斯任务
bool TaskWls(int index, int taskIndex) {
	CDm* dm = g_info[index].dm;
	CString strList = L"关闭广告|关闭广告1|登录确定";
	g_info[index].key = 18;
	long x, y;
	bool bFindComputer = false;
	Sleep(2000);
	while (g_flag)
	{
		if (!bFindComputer) {
			for (size_t i = 0; i < 5; i++)
			{
				RunScriptList(index, strList);
				bFindComputer = FindComputer(index, taskIndex);
				if (bFindComputer) {
					break;
				}
			}
			bFindComputer = true;
		}
		RunScriptList(index, strList);
		//关闭组队
		CloseTearm(index);
		//位置在交易地图则退出交易地图
		if (dm->RunScript(L"交易地图")) {
			dm->KeyPressChar(L"up");
		}
		//执行进入wls操作
		if (dm->RunScript(L"冰峰雪域地图") || dm->RunScript(L"导航按钮")) {
			ChoseBoss(index, L"选中乌鲁斯", 92, 260);//选择乌鲁斯
		}
		//退出扎昆
		ExitZkjt(index);
		ExitZkrk(index);
		if (g_AccountFile.GetInt(NumberToString(taskIndex + 1), L"state")==0 && bFindComputer) {
			
			if (dm->RunScript(L"乌鲁斯地图")) {
				ShowLog(L"查找npc");
				if (dm->RunScript(L"乌鲁斯") || dm->RunScript(L"乌鲁斯1") || dm->RunScript(L"乌鲁斯2")) {
					for (size_t i = 0; i < 5; i++)
					{
						ShowLog(L"查找选项");
						dm->RunScript(L"乌鲁斯选择");
						ScriptDelay(index, RandomNumber(300, 500));
						if (dm->RunScript(L"乌鲁斯下一步")) {
							dm->KeyPressChar(L"space");
							ScriptDelay(index, RandomNumber(100, 200));
						}
						if (dm->RunScript(L"乌鲁斯下一步1")) {
							dm->KeyPressChar(L"space");
							ScriptDelay(index, RandomNumber(100, 200));
						}
						if (dm->RunScript(L"退出队伍窗口")) {
							dm->KeyPressChar(L"space");
							ScriptDelay(index, RandomNumber(500, 800));
							CloseTearm(index);
						}
						int num = dm->GetColorNum(66, 573, 510, 749, L"44ddff", 1);
						ShowLog(L"下一步数量:"+NumberToString(num));
						if (num > 1000) {
							dm->KeyPressChar(L"space");
						}
					}
				}
				else
				{
					dm->KeyDownChar(L"right");
					ScriptDelay(index, RandomNumber(3000, 3500));
					dm->KeyUpChar(L"right");
				}
				if (dm->RunScript(L"等待组队")) {
					ScriptDelay(index, RandomNumber(1000,1500));
					dm->MoveTo(RandomNumber(200, 1000), RandomNumber(1, 400));
					bool bExitwls = false;
					g_bloodState = true;
					int k = 1;
					while (g_flag)
					{
						dm->RunScript(L"乌鲁斯选择");
						if (dm->RunScript(L"乌鲁斯下一步")) {
							dm->KeyPressChar(L"space");
							ScriptDelay(index, RandomNumber(100, 200));
						}
						if (dm->RunScript(L"乌鲁斯下一步1")) {
							dm->KeyPressChar(L"space");
							ScriptDelay(index, RandomNumber(100, 200));
						}
						if (k > 100) {
							dm->MoveTo(RandomNumber(200, 1000), RandomNumber(50,300));
							k = 1;
						}
						ShowLog(L"进入等待环节");
						dm->RunScript(L"等待组队");
						if (dm->RunScript(L"组队确定")) {
							ScriptDelay(index, RandomNumber(200, 300));
							dm->MoveTo(RandomNumber(200, 1000), RandomNumber(1, 300));
						}
						if (dm->RunScript(L"组队取消")) {
							ScriptDelay(index, RandomNumber(200, 300));
							dm->MoveTo(RandomNumber(200, 1000), RandomNumber(1, 300));
						}
						if (dm->RunScript(L"进入乌鲁斯")) {
							bExitwls = bossAttack(index, taskIndex);
						}
						if (dm->RunScript(L"乌鲁斯结束") || bExitwls) {
							Sleep(9000);
							ExitWls(index);
							break;
						}
						k++;
					}
				}
			}
		}

		if (dm->RunScript(L"乌鲁斯结束1")) {
			g_bloodState = false;
			bFindComputer = false;
			ShowLog(L"乌鲁斯结束");
			dm->KeyPressChar(L"esc");
			Sleep(300);
			for (size_t i = 0; i < 5; i++)
			{
				if (dm->RunScript(L"乌鲁斯结束1")) {
					Sleep(100);
					dm->KeyPressChar(L"esc");
					break;
				}
				Sleep(100);
			}
			g_main_cwnd->m_list_info.SetItemText(taskIndex, 6, L"完成");//当前账号置完成
			taskIndex = taskIndex + 1;
			g_AccountFile.SetString(NumberToString(taskIndex), L"state", L"1");
			int nLineCode = g_IniFile.GetInt(L"Set", L"CodeLine");//当前配置线路
			int nLineCode1 = _wtoi(g_main_cwnd->m_list_info.GetItemText(taskIndex , 12));
			if (nLineCode == nLineCode1) {
				//切换角色
				SwicthRole(index, taskIndex);
			}
			else {
				return true;
			}
		}
	}
	return false;
}

//退出乌鲁斯地图
void ExitWlsMap(int index, int taskIndex) {
	CDm* dm = g_info[index].dm;
	if (dm->RunScript(L"乌鲁斯地图")) {
		dm->KeyDownChar(L"left");//向左按住
		Sleep(2000);
		for (size_t i = 0; i < 20; i++)
		{
			dm->KeyDownChar(L"up");
			Sleep(RandomNumber(80, 90));
			dm->KeyUpChar(L"up");
		}
		dm->KeyUpChar(L"left");//向左弹起
		//dm->KeyUp(38);
		Sleep(RandomNumber(2000,3000));
		if (dm->RunScript(L"乌鲁斯地图")) {
			dm->KeyDownChar(L"right");//向右按住
			for (size_t i = 0; i < 20; i++)
			{
				dm->KeyDownChar(L"up");
				Sleep(RandomNumber(80, 90));
				dm->KeyUpChar(L"up");
			}
			dm->KeyUpChar(L"right");//向左弹起
		}

	}
}

//组队处理
bool ZuDuiChuLi(int index, int taskIndex) {
	long x, y;
	CDm* dm = g_info[index].dm;
	//导航窗口存在则先关闭
	if (dm->RunScript(L"导航窗口")) {
		dm->KeyPressChar(L"p");
	}
	//组队血条大于2个则取消组队重新组队
	//如果只有一个组队血条则返回真
	CString strXue = dm->FindImgEx(L"血条");
	if (strXue.Find(L"|") > 0) {//多个血条
		CloseTearm(index);//退出队伍
	}
	if (strXue == L"") {
		dm->KeyPressChar(L"p");
		Sleep(RandomNumber(500, 800));
		if (dm->RunScript(L"导航窗口",&x,&y)) {
			//dm->RunScript(L"添加组队");
			dm->MoveTo(x - 30 + RandomNumber(10, 20), y + 330 + RandomNumber(1, 10));
			Sleep(RandomNumber(50, 150));
			dm->LeftClick();
			Sleep(RandomNumber(500, 1500));
			dm->RunScript(L"确定组队");
		}
	}
	g_main_cwnd->AppendText(strXue);
	g_main_cwnd->AppendText(NumberToString(strXue.Find(L"|")));
	if (strXue != L"" && strXue.GetLength() > 0 && strXue.Find(L"|") == -1) {
		if (dm->RunScript(L"导航窗口", &x, &y)) {//关闭组队窗口
			dm->KeyPressChar(L"p");
		}
		return true;
	}
	return false;
}

//退出自由市场
void ExitShiChang(int index, int taskIndex) {
	CDm* dm = g_info[index].dm;
	if (dm->RunScript(L"自由市场")) {
		dm->KeyDownChar(L"left");//向左按住
		Sleep(RandomNumber(60, 90));
		for (size_t i = 0; i < 20; i++)
		{
			dm->KeyDownChar(L"up");
			Sleep(RandomNumber(80, 90));
			dm->KeyUpChar(L"up");
		}
		dm->KeyUpChar(L"left");//向左弹起
							   //dm->KeyUp(38);
		Sleep(RandomNumber(1000, 2000));
		if (dm->RunScript(L"自由市场")) {
			dm->KeyDownChar(L"right");//向右按住
			Sleep(500);
			for (size_t i = 0; i < 20; i++)
			{
				dm->KeyDownChar(L"up");
				Sleep(RandomNumber(80, 90));
				dm->KeyUpChar(L"up");
			}
			Sleep(RandomNumber(2000, 3000));
			dm->KeyUpChar(L"right");//向左弹起
		}

	}
}

//换线
bool HuanXian(int index, int taskIndex) {
	long x, y;
	CDm* dm = g_info[index].dm;
	dm->FindPic(558, 384, 938, 643, L"huanxian.bmp", L"202020", 0.95, 0, &x, &y);
	if (x == -1) {
		Sleep(RandomNumber(200, 300));
		dm->KeyPress(27);
		Sleep(RandomNumber(200, 300));
	}
	else {
		Sleep(RandomNumber(200, 300));
		dm->KeyPress(13);
		Sleep(RandomNumber(200, 300));
	}
	dm->FindPic(611, 232, 695, 299, L"xq9.bmp", L"202020", 0.95, 0, &x, &y);
	if (x > 0) {
		Sleep(1000);
		dm->MoveTo(x + 7, y + 5);
		Sleep(500);
		dm->LeftDoubleClick();
		Sleep(500);
		Sleep(8000);
		return true;
	}
	dm->FindPic(611, 232, 695, 299, L"xq91.bmp", L"202020", 0.90, 0, &x, &y);
	if (x > 0) {
		Sleep(1000);
		dm->KeyPress(27);
		Sleep(1000);
		return true;
	}
	return false;
}

//移动进入祭坛入口
void YiDongJiTan(int index, int taskIndex) {
	CDm* dm = g_info[index].dm;
	if (movetoX(index, 140)) {
		if (!dm->RunScript(L"选择进入祭坛")) {//不存在选择祭坛进行移动
			dm->KeyDownChar(L"right");
			Sleep(50);
			for (size_t i = 0; i < 20; i++)
			{
				dm->KeyPress(38);
				if (dm->RunScript(L"选择进入祭坛")) {
					break;
				}
			}
			dm->KeyUpChar(L"right");
			Sleep(RandomNumber(6000,7000));
			if (dm->RunScript(L"扎昆祭台入口")) {
				return;
			}
			else {
				dm->KeyDownChar(L"left");
				for (size_t i = 0; i < 20; i++)
				{
					dm->KeyPress(38);
					if (dm->RunScript(L"选择进入祭坛")) {
						break;
					}
				}
				Sleep(RandomNumber(500, 1000));
				dm->KeyUpChar(L"left");
			}
		}
		
	}
}

//导航扎昆地图
void DaoHangZhaKun(int index){
	CDm* dm = g_info[index].dm;
	if (dm->RunScript(L"导航扎昆")) {
		Sleep(1000);
		dm->RunScript(L"go");
		Sleep(1000);
		if (dm->RunScript(L"扎昆已刷")) {
			g_nState = 9; //设定已完成
		}
		for (size_t i = 0; i < 5; i++)
		{
			if (dm->RunScript(L"确定按钮")) {
				Sleep(2000);
			}
		}
	}
	else {
		dm->KeyPressChar(L"t");
	}
}

//移动红球
bool MoveQiu(int index) {
	CDm* dm = g_info[index].dm;
	long x, y;
	if (dm->RunScript(L"背包")) {
		if (dm->RunScript(L"背包缩小", 0)) {
			if (dm->RunScript(L"背包", &x, &y)) {//点击消耗栏
				if (dm->RunScript(L"消耗栏")) {
					if (dm->RunScript(L"红球", &x, &y)) {//点击消耗栏
						dm->MoveTo(x + 20 + RandomNumber(1, 5), y + 3 + RandomNumber(1, 5));
						dm->LeftClick();
						ScriptDelay(index, RandomNumber(300, 500));
						dm->MoveTo(RandomNumber(600, 620), RandomNumber(550, 650));
						ScriptDelay(index, RandomNumber(800, 1000));
						dm->LeftClick();
						ScriptDelay(index, RandomNumber(1000, 1500));
						CString strNum;
						for (size_t i = 0; i < 10; i++)
						{
							int nColNum = dm->GetColorNum(200, 569, 814, 670, L"e0cfaa-050505", 0.95);
							if (nColNum>800) {
								strNum.Format(L"颜色数量:%d", nColNum);
								g_main_cwnd->AppendText(strNum);
								dm->KeyPress(73);
								Sleep(200);
								return true;
							}
							Sleep(100);
						}
					}
				}
				else {
					dm->MoveTo(x - 255 + RandomNumber(1, 5), y + 25 + RandomNumber(1, 5));
					ScriptDelay(index, RandomNumber(300, 500));
					dm->LeftClick();
				}
			}
		}
		else {
			dm->RunScript(L"背包扩大");
		}
	}
	else {
		dm->KeyPressChar(L"i");//打开背包
	}
	return false;
}

void ExitZhaKunJiTan(int index) {
	CDm* dm = g_info[index].dm;
}

void QingLiZhuangBei(int index) {
	CDm *dm = g_info[index].dm;
	long x, y,x1,y1;
	bool bState = false;
	for (size_t j = 0; j < 10; j++)
	{
		if (bState)//清理成功退出市场
		{

		}
		else {
			if (dm->RunScript(L"自由市场")) {//进入自由市场
				
				if (dm->RunScript(L"清理装备窗口", &x1, &y1)) {
					if (dm->RunScript(L"装备清理完成")) {
						bState = true;
					}
					if (dm->RunScript(L"市场装备栏")) {//找到装备栏
						Sleep(RandomNumber(1000, 2000));
						dm->MoveToEx(x1 + 250, y1 + 98, 5, 5); //选中其他栏目清理
						Sleep(RandomNumber(200, 300));
						dm->LeftClick();
						Sleep(RandomNumber(200, 300));
						if (dm->RunScript(L"市场其他栏目")) {//选中其他栏目

						}
						
					}
					else {//未找到装备栏

					}
				}
				else
				{
					dm->RunScript(L"npc收藏家");
				}
			}
			else {
				if (dm->RunScript(L"导航按钮", &x, &y)) {
					dm->MoveToEx(x + 10, y + 10, 10, 5);
					dm->Delays(100, 200);
					dm->LeftClick();
					Sleep(RandomNumber(550, 800));
					dm->MoveTo(x + 220+RandomNumber(1,10), y + 300+RandomNumber(1, 35));
					if (dm->RunScript(L"快速导航", &x, &y)) {
						Sleep(RandomNumber(550, 800));
						dm->MoveToEx(x - 35, y + 104, 10, 5);
						Sleep(RandomNumber(150, 200));
						dm->LeftClick();
						Sleep(RandomNumber(150, 200));
					}
				}
			}
			Sleep(200);
		}

	}
}

bool TaskZhaKun(int index, int taskIndex) {

	CDm* dm = g_info[index].dm;
	CString strList = L"关闭广告|关闭广告1|登录确定";
	long x, y;
	long count=0,j=0;
	bool bIsLine = false; //是否切换线路
	bool bIsTeam = false; //进入地图后组队是否变更
	int nState = 0;
	while (g_flag)
	{
		if (count % 10 == 0) {
			dm->MoveTo(RandomNumber(1, 1000), RandomNumber(1, 700));
		}
		RunScriptList(index, strList);

		if (g_nState == 0) {
			//退出交易地图
			if (dm->RunScript(L"交易地图")) {
				dm->KeyPressChar(L"up");
				//根据状态处理 为0 退出地图 boss已完成 进行交易
			}
			//退出564地图
			ExitWlsMap(index, taskIndex);
			//退出自由市场
			ExitShiChang(index, taskIndex);
			//到达扎昆入口 切换线路
			if (dm->RunScript(L"扎昆入口")) {
				//进入地图后先处理组队
				if (!bIsTeam) {
					bIsTeam = ZuDuiChuLi(index, taskIndex);//组队是否只开启一个
				}
				else {
					if (!bIsLine) {
						bIsLine = HuanXian(index, taskIndex);//线路是否已经切换
					}
					else
					{
						YiDongJiTan(index, taskIndex);//移动进入扎昆祭坛入口
					}
				}


			}
			//到达村庄
			if (dm->RunScript(L"村庄地图")) {
				DaoHangZhaKun(index);
			}

		}


		if (dm->RunScript(L"扎昆祭台入口")) {
			//对话npc //如果已刷变更状态
			if (dm->RunScript(L"npc啊杜比斯")) {//找到npc
				for (size_t i = 0; i < 10; i++)//循环查找下一步
				{
					if (dm->RunScript(L"npc啊杜比斯")) {
						if (dm->RunScript(L"下一步")) {
							dm->KeyPressChar(L"space");
							ScriptDelay(index, RandomNumber(30, 50));
							if (dm->RunScript(L"扎昆已刷")) {
								//变更状态
							}
						}
					}
				}
			}
		}


		//到达扎昆地图
		if (dm->RunScript(L"扎昆祭台地图")) {
			if (nState == 0) {//开启技能键
				ScriptDelay(index, RandomNumber(500, 1000));
				dm->KeyPressChar(L"1");
				ScriptDelay(index, RandomNumber(3000, 4000));
				nState = 1;
			}
			if (nState == 1) {//移动到中心 看情况更改模式
				if (dm->RunScript(L"移动标记")) {
					dm->KeyDownChar(L"right");
					ScriptDelay(index, RandomNumber(1800, 1950));
					dm->KeyUpChar(L"right");
					ScriptDelay(index, RandomNumber(1000, 1550));
					//if (dm->RunScript(L"移动标记")) {//移动后还找到则重新激活窗口在移动
					//}
				}
				else {
					nState = 2;
				}
			}
			if (nState == 2) {
				//背包是否打开
				if (MoveQiu(index)) {
					nState = 3;
				}
			}
			if (nState == 3) {
				if (dm->RunScript(L"扎昆物品")) {
					g_nState = 4;
				}
				g_main_cwnd->AppendText(L"开始攻击");
				for (size_t i = 0; i < RandomNumber(15, 20); i++)
				{
					dm->KeyDown(16);
					Sleep(RandomNumber(80, 120));
					dm->KeyUp(16);
					g_main_cwnd->AppendText(L"攻击中");
				}
				g_main_cwnd->AppendText(L"继续攻击");
			}
			if (nState == 4) {
				if (dm->RunScript(L"扎昆物品")) {
					dm->KeyDown(37);
					Sleep(RandomNumber(100, 200));

					for (size_t i = 0; i < RandomNumber(45, 50); i++)
					{
						dm->KeyDown(90);
						Sleep(RandomNumber(80, 120));
						dm->KeyUp(90);
					}
					dm->KeyUp(37);
					dm->KeyUp(90);
					Sleep(RandomNumber(400, 600));

					dm->KeyDown(39);
					Sleep(RandomNumber(100, 200));
					for (size_t i = 0; i < RandomNumber(50, 55); i++)
					{
						dm->KeyDown(90);
						Sleep(RandomNumber(80, 120));
						dm->KeyUp(90);
					}
					Sleep(RandomNumber(1300, 1500));
					dm->KeyUp(39);
					dm->KeyUp(90);
				}
				else {
					j++;
				}
				Sleep(500);
				if (j > 5) {
					nState = 5;
					j = 0;
				}
			}
		}

		if (nState == 5) {
			if (dm->RunScript(L"扎昆祭台地图")) {
				Sleep(500);
				dm->KeyPress(32);
				Sleep(500);
				dm->RunScript(L"确认退出祭坛");
			}
			if (dm->RunScript(L"扎昆祭台入口")) {
				ExitZkjt(index);
			}
			if (dm->RunScript(L"扎昆入口")) {
				ExitZkrk(index);
			}
			if (dm->RunScript(L"村庄地图")) {
				//清理装备

			}
		}
		

		//清理装备
		if (dm->RunScript(L"地图")) {
		}

		


		if (count > 30 && g_nState==0) {//次数超过执行查找boss点击
			if (!dm->RunScript(L"扎昆入口") && dm->RunScript(L"扎昆入口")) {
				dm->KeyPressChar(L"t");
				Sleep(1000);
				DaoHangZhaKun(index);
				count = 0;
			}

		}
		++count;//执行次数
	}
	return true;
}

//扎昆boss任务
bool ZhaKunAttack(int index, int taskIndex) {
	return true;
}
